package com.bocai.eelly.activitys;

import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bocai.eelly.R;
import com.bocai.eelly.bean.BaseBean;
import com.bocai.eelly.common.BaseActivity;
import com.bocai.eelly.common.Loading;
import com.bocai.eelly.common.ToolbarHelper;
import com.bocai.eelly.utils.L;
import com.bocai.eelly.utils.T;
import com.bocai.eelly.utils.Utils;

import butterknife.Bind;
import butterknife.ButterKnife;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

public class FeedbackActivity extends BaseActivity {

    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.toolbar)
    Toolbar toolbar;
    @Bind(R.id.et_feedback)
    EditText etFeedback;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_feedback);
        ButterKnife.bind(this);
        initView();
        iniEvent();
    }

    private void iniEvent() {
        toolbar.inflateMenu(R.menu.menu_feedback);
        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                if(Utils.isEmpty(etFeedback.getText().toString()))
                {
                    T.showShort(FeedbackActivity.this,"您还未填写任何内容");
                }else{

                    feedback();
                }
                return false;
            }
        });
    }

    private void feedback() {
        Loading.show(this,getString(R.string.str_loading));
        String timeline= Utils.getTimeline();
        String sign=Utils.md5(timeline);
        getTestApi().feedback(timeline,sign,Utils.getUserId(this,"id"),etFeedback.getText().toString())
                .compose(this.<BaseBean>bindToLifecycle())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<BaseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Loading.dismiss();
                        L.d(e.toString());
                        T.showShort(FeedbackActivity.this,R.string.net_error);
                    }

                    @Override
                    public void onNext(BaseBean baseBean) {
                        Loading.dismiss();
                        T.showShort(FeedbackActivity.this,baseBean.getMsg());
                    }
                });

    }

    private void initView() {
        ToolbarHelper.setup(this, R.string.str_title_feedback, R.mipmap.ic_back, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
    }
}
